package design.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * 被观察者
 */
public class Observable {

    //1-记录观察者
    private List<ObserverService> observers = new ArrayList<ObserverService>();

    public Boolean addObserver(ObserverService observerService) {
        return observers.add(observerService);
    }

    //2-发生改变的动作
    public void changed() {
        System.out.println("被观察者已发生变化   ... ... ");
        notifyObserver();
    }

    //3-通知观察者
    private void notifyObserver() {
        System.out.println("被观察者开始通知观察者   ... ... ");
        for (ObserverService observerService : observers) {
            observerService.update();
        }
    }

}
